package com.ruoyi.web.controller.train;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.TrainingMedicalRecord;
import com.ruoyi.system.domain.TrainingMedicalRecordCount;
import com.ruoyi.system.service.ITrainingMedicalRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 临床医案Controller
 *
 * @author dp
 * @date 2023-11-27
 */
@Api(tags = {"临床医案"})
@RestController
@RequestMapping("/train/record")
public class TrainingMedicalRecordController extends BaseController {
    @Autowired
    private ITrainingMedicalRecordService trainingMedicalRecordService;
    @Autowired
    TokenService tokenService;

    /**
     * 查询临床医案列表
     */
    @ApiOperation(value = "临床医案列表", notes = "查询临床医案列表详情", tags = {"临床医案管理"})
    //@PreAuthorize("@ss.hasPermi('system:record:list')")
    @GetMapping("/list")
    public TableDataInfo list(TrainingMedicalRecord trainingMedicalRecord) {
        startPage();
        List<TrainingMedicalRecord> list = trainingMedicalRecordService.selectTrainingMedicalRecordList(trainingMedicalRecord);
        return getDataTable(list);
    }

    /**
     * 查询临床医案合计列表
     */
    @ApiOperation(value = "临床医案合计列表", notes = "查询临床医案列表详情", tags = {"临床医案管理"})
    //@PreAuthorize("@ss.hasPermi('system:record:list')")
    @GetMapping("/countList")
    public TableDataInfo countList(@RequestParam(value = "submitBegin", required = false) String submitBegin,
                                   @RequestParam(value = "submitEnd", required = false) String submitEnd,
                                   @RequestParam("baseId") String baseId,
                                   @RequestParam("medicalRecordStatus") String RecordStatus,
                                   @RequestParam(value = "cadetId", required = false) Long cadetId,
                                   @RequestParam(value = "cadetName", required = false) String cadetName) {
        startPage();
        List<TrainingMedicalRecordCount> list = trainingMedicalRecordService.selectTrainingMedicalRecordCount(
                submitBegin, submitEnd, baseId, RecordStatus, cadetId, cadetName);
        return getDataTable(list);
    }

    /**
     * 根据条件获取临床医案列表
     *
     * @param submitBegin
     * @param submitEnd
     * @param baseId
     * @param cadetId
     * @param RecordStatus
     * @return
     */
    @ApiOperation(value = "根据条件获取列表", notes = "根据条件获取列表", tags = {"临床医案管理"})
    @GetMapping("/getRecordList")
    public TableDataInfo getRecordList(@RequestParam(value = "submitBegin",required = false) String submitBegin,
                                       @RequestParam(value = "submitEnd",required = false) String submitEnd,
                                       @RequestParam("baseId") String baseId, @RequestParam("cadetId") String cadetId,
                                       @RequestParam("medicalRecordStatus") String RecordStatus) {
        startPage();
        List<TrainingMedicalRecord> list = trainingMedicalRecordService.getRecordList(submitBegin, submitEnd, baseId, cadetId, RecordStatus);
        return getDataTable(list);
    }

    /**
     * 导出临床医案列表
     */
    @ApiOperation(value = "临床医案列表导出", notes = "导出临床医案列表", tags = {"临床医案管理"})
    //@PreAuthorize("@ss.hasPermi('system:record:export')")
    @Log(title = "临床医案", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TrainingMedicalRecord trainingMedicalRecord) {
        List<TrainingMedicalRecord> list = trainingMedicalRecordService.selectTrainingMedicalRecordList(trainingMedicalRecord);
        ExcelUtil<TrainingMedicalRecord> util = new ExcelUtil<TrainingMedicalRecord>(TrainingMedicalRecord.class);
        util.exportExcel(response, list, "临床医案数据");
    }

    /**
     * 获取临床医案详细信息
     */
    @ApiOperation(value = "临床医案详细信息", notes = "获取临床医案详细信息详情", tags = {"临床医案管理"})
    //@PreAuthorize("@ss.hasPermi('system:record:query')")
    @GetMapping(value = "/{medicalRecordId}")
    public AjaxResult getInfo(@PathVariable("medicalRecordId") Long medicalRecordId) {
        return success(trainingMedicalRecordService.selectTrainingMedicalRecordByMedicalRecordId(medicalRecordId));
    }

    /**
     * 新增临床医案
     */
    @ApiOperation(value = "新增临床医案", notes = "新增临床医案详情", tags = {"临床医案管理"})
    //@PreAuthorize("@ss.hasPermi('system:record:add')")
    @Log(title = "临床医案", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(HttpServletRequest request, @RequestBody TrainingMedicalRecord trainingMedicalRecord) {
        Long userId = tokenService.getLoginUser(request).getUser().getUserId();
        trainingMedicalRecordService.completeData(trainingMedicalRecord, userId, 1);
        return toAjax(trainingMedicalRecordService.insertTrainingMedicalRecord(trainingMedicalRecord));
    }

    /**
     * 修改临床医案
     */
    @ApiOperation(value = "修改临床医案", notes = "修改临床医案详情", tags = {"临床医案管理"})
    //@PreAuthorize("@ss.hasPermi('system:record:edit')")
    @Log(title = "临床医案", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(HttpServletRequest request, @RequestBody TrainingMedicalRecord trainingMedicalRecord) {
        Long userId = tokenService.getLoginUser(request).getUser().getUserId();
        trainingMedicalRecordService.completeData(trainingMedicalRecord, userId, 2);
        return toAjax(trainingMedicalRecordService.updateTrainingMedicalRecord(trainingMedicalRecord));
    }

    /**
     * 删除临床医案
     */
    @ApiOperation(value = "删除临床医案", notes = "删除临床医案详情", tags = {"临床医案管理"})
    //@PreAuthorize("@ss.hasPermi('system:record:remove')")
    @Log(title = "临床医案", businessType = BusinessType.DELETE)
    @DeleteMapping("/{medicalRecordIds}")
    public AjaxResult remove(@PathVariable Long[] medicalRecordIds) {
        return toAjax(trainingMedicalRecordService.deleteTrainingMedicalRecordByMedicalRecordIds(medicalRecordIds));
    }

}
